<html>
<head>
<title>SpecialAdapter.java</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
.ln { color: rgb(0,0,0); font-weight: normal; font-style: normal; }
.s0 { color: rgb(0,0,128); font-weight: bold; }
.s1 { }
.s2 { color: rgb(128,128,128); font-style: italic; }
.s3 { color: rgb(128,128,128); font-weight: bold; font-style: italic; }
.s4 { color: rgb(0,0,255); }
</style>
</head>
<BODY BGCOLOR="#ffffff">
<TABLE CELLSPACING=0 CELLPADDING=5 COLS=1 WIDTH="100%" BGCOLOR="#C0C0C0" >
<TR><TD><CENTER>
<FONT FACE="Arial, Helvetica" COLOR="#000000">
SpecialAdapter.java</FONT>
</center></TD></TR></TABLE>
<pre>

<span class="s0">package </span><span class="s1">com.example.lista_zakupow.adapters; 
 
</span><span class="s0">import </span><span class="s1">android.content.Context; 
</span><span class="s0">import </span><span class="s1">android.graphics.Color; 
</span><span class="s0">import </span><span class="s1">android.view.LayoutInflater; 
</span><span class="s0">import </span><span class="s1">android.view.View; 
</span><span class="s0">import </span><span class="s1">android.view.ViewGroup; 
</span><span class="s0">import </span><span class="s1">android.widget.BaseAdapter; 
</span><span class="s0">import </span><span class="s1">android.widget.TextView; 
</span><span class="s0">import </span><span class="s1">com.example.lista_zakupow.R; 
 
</span><span class="s0">import </span><span class="s1">java.util.ArrayList; 
</span><span class="s0">import </span><span class="s1">java.util.List; 
 
</span><span class="s2">/** 
 * Adapter ktory używa lista z zakupami/ produktami 
 * </span><span class="s3">@param </span><span class="s1">&lt;</span><span class="s2">T</span><span class="s1">&gt; </span><span class="s2">element listy 
 */</span><span class="s1"> 
</span><span class="s0">public class </span><span class="s1">SpecialAdapter&lt;T </span><span class="s0">extends </span><span class="s1">GetterName&gt; </span><span class="s0">extends </span><span class="s1">BaseAdapter { 
    </span><span class="s0">private </span><span class="s1">LayoutInflater mInflater; 
    </span><span class="s0">private </span><span class="s1">List&lt;T&gt; data; 
    </span><span class="s0">private </span><span class="s1">List&lt;Boolean&gt; selected; 
 
    </span><span class="s2">/** 
     * Konstruktor klasy SpecialAdapter 
     * </span><span class="s3">@param </span><span class="s2">context obecny kontekst 
     * </span><span class="s3">@param </span><span class="s2">listaZakupow listaZZakupami 
     */</span><span class="s1"> 
    </span><span class="s0">public </span><span class="s1">SpecialAdapter(Context context, List&lt;T&gt; listaZakupow) { 
        mInflater = LayoutInflater.from(context); 
        </span><span class="s0">this</span><span class="s1">.data = listaZakupow; 
        selected = </span><span class="s0">new </span><span class="s1">ArrayList&lt;Boolean&gt;(); 
        </span><span class="s0">for </span><span class="s1">(</span><span class="s0">int </span><span class="s1">i = </span><span class="s4">0</span><span class="s1">; i &lt; listaZakupow.size(); i++) 
            selected.add(</span><span class="s0">false</span><span class="s1">); 
    } 
 
    </span><span class="s2">/** 
     * Zwraca ilosc elementow listy 
     * </span><span class="s3">@return </span><span class="s2">ilosc elementow listy 
     */</span><span class="s1"> 
    </span><span class="s0">public int </span><span class="s1">getCount() { 
        </span><span class="s0">return </span><span class="s1">data.size(); 
    } 
 
    </span><span class="s2">/** 
     * Zwraca element listy 
     * </span><span class="s3">@param </span><span class="s2">i pozycja 
     * </span><span class="s3">@return </span><span class="s2">element listy 
     */</span><span class="s1"> 
    </span><span class="s0">public </span><span class="s1">T getItem(</span><span class="s0">int </span><span class="s1">i) { 
        </span><span class="s0">return </span><span class="s1">data.get(i); 
    } 
 
    </span><span class="s2">/** 
     * Usuwa element 
     * </span><span class="s3">@param </span><span class="s2">position pozycja usuwaneo elementu 
     */</span><span class="s1"> 
    </span><span class="s0">public void </span><span class="s1">removeItem(</span><span class="s0">int </span><span class="s1">position) { 
        data.remove(position); 
        selected.remove(position); 
        notifyDataSetChanged(); 
    } 
 
    </span><span class="s2">/** 
     * Dodaje element do listy 
     * </span><span class="s3">@param </span><span class="s2">item element do dodania 
     */</span><span class="s1"> 
    </span><span class="s0">public void </span><span class="s1">addItem(T item) { 
        </span><span class="s0">this</span><span class="s1">.data.add(item); 
        </span><span class="s0">this</span><span class="s1">.selected.add(</span><span class="s0">false</span><span class="s1">); 
        notifyDataSetChanged(); 
    } 
 
    </span><span class="s2">/** 
     * Zwraca id elementu 
     * </span><span class="s3">@param </span><span class="s2">position pozycja szukanego elementu 
     * </span><span class="s3">@return </span><span class="s2">id elementu 
     */</span><span class="s1"> 
    </span><span class="s0">public long </span><span class="s1">getItemId(</span><span class="s0">int </span><span class="s1">position) { 
        </span><span class="s0">return </span><span class="s1">position; 
    } 
 
    </span><span class="s2">/** 
     * Ustawia zaznaczenie produktu 
     * </span><span class="s3">@param </span><span class="s2">pos pozycja elementu do zaznaczenia 
     */</span><span class="s1"> 
    </span><span class="s0">public void </span><span class="s1">setSelected(</span><span class="s0">int </span><span class="s1">pos) { 
        unselectAll(); 
        </span><span class="s0">this</span><span class="s1">.selected.set(pos, </span><span class="s0">true</span><span class="s1">); 
        notifyDataSetChanged(); 
    } 
 
    </span><span class="s2">/** 
     * Odznaczenie wszystkich elementow 
     */</span><span class="s1"> 
    </span><span class="s0">public void </span><span class="s1">unselectAll() { 
        </span><span class="s0">for </span><span class="s1">(</span><span class="s0">int </span><span class="s1">i = </span><span class="s4">0</span><span class="s1">; i &lt; selected.size(); i++) 
            </span><span class="s0">this</span><span class="s1">.selected.set(i, </span><span class="s0">false</span><span class="s1">); 
        notifyDataSetChanged(); 
    } 
 
    </span><span class="s2">/** 
     * Odznaczenie elementu 
     * </span><span class="s3">@param </span><span class="s2">pos pozycja elementu do odznaczenia 
     */</span><span class="s1"> 
    </span><span class="s0">public void </span><span class="s1">unselect(</span><span class="s0">int </span><span class="s1">pos) { 
        </span><span class="s0">this</span><span class="s1">.selected.set(pos, </span><span class="s0">false</span><span class="s1">); 
        notifyDataSetChanged(); 
    } 
 
    </span><span class="s2">/** 
     * Czy jest jakis element na liscie zaznaczony 
     * </span><span class="s3">@return </span><span class="s2">true or false w zaleznosci czy jais element jest zaznaczony 
     */</span><span class="s1"> 
    </span><span class="s0">public boolean </span><span class="s1">isAnySelected() { 
        </span><span class="s0">for </span><span class="s1">(Boolean selectedVal : selected) { 
            </span><span class="s0">if </span><span class="s1">(selectedVal) 
                </span><span class="s0">return true</span><span class="s1">; 
        } 
        </span><span class="s0">return false</span><span class="s1">; 
    } 
 
    </span><span class="s2">/** 
     * Zwraca widok elementu listy 
     * </span><span class="s3">@param </span><span class="s2">position pozycja 
     * </span><span class="s3">@param </span><span class="s2">convertView widok 
     * </span><span class="s3">@param </span><span class="s2">parent ojciec 
     * </span><span class="s3">@return </span><span class="s2">widok 
     */</span><span class="s1"> 
    </span><span class="s0">public </span><span class="s1">View getView(</span><span class="s0">int </span><span class="s1">position, View convertView, ViewGroup parent) { 
        ViewHolder holder; 
        </span><span class="s0">if </span><span class="s1">(convertView == </span><span class="s0">null</span><span class="s1">) { 
            convertView = mInflater.inflate(R.layout.row, </span><span class="s0">null</span><span class="s1">); 
            holder = </span><span class="s0">new </span><span class="s1">ViewHolder(); 
            holder.text = (TextView) convertView.findViewById(R.id.headline); 
            convertView.setTag(holder); 
        } </span><span class="s0">else </span><span class="s1">{ 
            holder = (ViewHolder) convertView.getTag(); 
        } 
 
        holder.text.setText(data.get(position).getNameToView()); 
        convertView.setBackgroundColor(Color.BLACK); 
 
        </span><span class="s0">if</span><span class="s1">(selected.get(position)) 
        { 
            convertView.setBackgroundColor(Color.CYAN); 
        } 
 
        </span><span class="s0">return </span><span class="s1">convertView; 
    } 
 
    </span><span class="s2">/** 
     * zwraca wybrany element 
     * </span><span class="s3">@return </span><span class="s2">wybrany element 
     */</span><span class="s1"> 
    </span><span class="s0">public </span><span class="s1">T getSelectedItem() { 
        </span><span class="s0">for </span><span class="s1">(</span><span class="s0">int </span><span class="s1">i = </span><span class="s4">0</span><span class="s1">; i &lt; selected.size(); i++) { 
            </span><span class="s0">if </span><span class="s1">(selected.get(i)) 
                </span><span class="s0">return </span><span class="s1">data.get(i); 
        } 
        </span><span class="s0">return null</span><span class="s1">; 
    } 
 
    </span><span class="s2">/** 
     * Zwraca pozycję wybranego elementu 
     * </span><span class="s3">@return </span><span class="s2">pozycja wybranego elementu 
     */</span><span class="s1"> 
    </span><span class="s0">public int </span><span class="s1">getSelectedItemPos() { 
        </span><span class="s0">for </span><span class="s1">(</span><span class="s0">int </span><span class="s1">i = </span><span class="s4">0</span><span class="s1">; i &lt; selected.size(); i++) { 
            </span><span class="s0">if </span><span class="s1">(selected.get(i)) 
                </span><span class="s0">return </span><span class="s1">i; 
        } 
        </span><span class="s0">return </span><span class="s1">-</span><span class="s4">1</span><span class="s1">; 
    } 
 
    </span><span class="s2">/** 
     * Widok ktory jest widoczny na liscie 
     */</span><span class="s1"> 
    </span><span class="s0">public static class </span><span class="s1">ViewHolder { 
        TextView text; 
    } 
} 
 
 
 
 
 
</span></pre>
</body>
</html>